﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace storeSMS
{
    public partial class frmSend : Form
    {
        public frmSend()
        {
            InitializeComponent();
        }

        private void frmSend_Load(object sender, EventArgs e)
        {
            dgvsendinfo.AutoGenerateColumns = false;
            dgreinfo.AutoGenerateColumns = false;
            BLL.sendinfo bll = new BLL.sendinfo();
            BLL.reinfo bllr = new BLL.reinfo();
            DataSet ds=new DataSet();
            DataTable dt1=bll.GetAllList().Tables[0].Copy();
            DataTable dt2 = bllr.GetAllList().Tables[0].Copy();
            dt1.TableName = "dt1";
            dt2.TableName = "dt2";
            ds.Tables.Add(dt1);
            ds.Tables.Add(dt2);
            DataRelation relation=new DataRelation("rel1",dt1.Columns["sendnum"],dt2.Columns["phone"],false);
            //ds.Merge(ds, true, MissingSchemaAction.AddWithKey);
            ds.Relations.Add(relation);
            BindingSource myBindingSource= new BindingSource();
            myBindingSource.DataSource=ds;
            myBindingSource.DataMember = "dt1";
            dgvsendinfo.DataSource = myBindingSource;
            //dgvsendinfo.datab
            BindingSource myBindingSources = new BindingSource();
            myBindingSources.DataSource = myBindingSource;
            myBindingSources.DataMember = "rel1";
            dgreinfo.DataSource = myBindingSources;
            
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string gys = txtgys.Text.Trim();
            string phone = txtphone.Text.Trim();
            string project = txtproject.Text.Trim();
            string ddh = txtddh.Text.Trim();
            string sql="";
            if (gys != "")
                sql += " and gys='" + gys + "'";
            if (phone != "")
                sql += " and phone='" + phone + "'";
            if (ddh != "")
                sql += " and cgddh='" + ddh + "'";
            if (project != "")
                sql += " and project='" + project + "'";
            BLL.sendinfo bll = new BLL.sendinfo();
            DataSet ds = bll.GetList(sql);
            dgvsendinfo.DataSource = ds.Tables[0];

        }

        private void 自动发送ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowindex = 0;
            rowindex = dgvsendinfo.CurrentRow.Index;
            string cgddh = "", project = "", gh = "", phone = "", sendcontent = "";
            int sid = 0;

            cgddh = dgvsendinfo.Rows[rowindex].Cells["cgddh"].Value.ToString();
            project = dgvsendinfo.Rows[rowindex].Cells["project"].Value.ToString();
                gh = dgvsendinfo.Rows[rowindex].Cells["gh"].Value.ToString();
                phone = dgvsendinfo.Rows[rowindex].Cells["phone"].Value.ToString();
                sid = int.Parse(dgvsendinfo.Rows[rowindex].Cells["sid"].Value.ToString());
                sendcontent = myinfo.smsinfo;
                sendcontent = sendcontent.Replace("cgddh", cgddh).Replace("project", project).Replace("gh", gh);
                SMSMessage.Send(phone, sendcontent, sid);           
        }

        private void 人工发送ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowindex = 0;
            rowindex = dgvsendinfo.CurrentRow.Index;
            string phone = "";
            
            phone = dgvsendinfo.Rows[rowindex].Cells["phone"].Value.ToString();
            frmSDSend f = new frmSDSend(phone);
            f.ShowDialog();
        }
    }
}
